<?php
 // 去掉WP版本号
remove_action('wp_head', 'wp_generator');

// 去掉评论内容网址超链
remove_filter('comment_text', 'make_clickable', 9);

// 禁用半角符号自动转换为全角
remove_filter('the_content', 'wptexturize');

// 缩略图
if ( function_exists('add_theme_support')){
	add_theme_support('post-thumbnails');
	//set_post_thumbnail_size( 500, 280, true );
	//add_image_size( 'focus-thumb', 500, 280, true );
}
// 保留段首空格 http://blog.programet.org/2011/04/keep-blanks.html
add_filter('tiny_mce_before_init', 'preserve_nbsp_chars');
function preserve_nbsp_chars($initArray) {
	$initArray['entities'] = '160,nbsp,'.$initArray['entities'];
	return $initArray;
}
// 计算UTF-8字符串长度（忽略字节的方案）http://www.phpq.net/tutorial/php-string-length.html
function strlen_utf8($str) { 
	$i = 0;
	$count = 0;
	$len = strlen($str);
	while ($i < $len) {
		$chr = ord($str[$i]);
		$count++;
		$i++;
		if($i >= $len) break;
		if($chr & 0x80) {
			$chr <<= 1;
			while ($chr & 0x80) {
				$i++;
				$chr <<= 1;
			}
		}
	}
	return $count;
}

// 去掉指定分类
function the_category_filter($thelist,$separator=' ') {
	if(!defined('WP_ADMIN')) {
		//要移除的分类ID
		$exclude = array(1);
		$exclude2 = array();
		foreach($exclude as $c) {
			$exclude2[] = get_cat_name($c);
		}
		$cats = explode($separator,$thelist);
		$newlist = array();
		foreach($cats as $cat) {
			$catname = trim(strip_tags($cat));
			if(!in_array($catname,$exclude2))
				$newlist[] = $cat;
		}
		return implode($separator,$newlist);
	} else {
		return $thelist;
	}
}
add_filter('the_category','the_category_filter', 10, 2);

// 去掉系统相册样式
add_filter( 'use_default_gallery_style', '__return_false' );

//修改登录界面
function change_wp_login_logo() {
	echo '<style type="text/css">h1 a { background-image:url('.get_bloginfo('template_directory').'/images/logo-login.png) !important; padding-bottom: 0 !important; }</style>';
}
function change_wp_login_url( $url ) {
	return get_bloginfo('url');
}
function change_wp_login_title() {
	return get_option('blogname');
}
add_action('login_head', 'change_wp_login_logo');
add_filter('login_headerurl', 'change_wp_login_url');
add_filter('login_headertitle', 'change_wp_login_title');

// WordPress防止评论冒充博主
function ludou_usecheck($incoming_comment) {
	$isSpam = 0;
	if (trim($incoming_comment['comment_author']) == 'h') $isSpam = 1;
	if (trim($incoming_comment['comment_author_email']) == 'h@youmeiwenzi.com') $isSpam = 1;
	if(!$isSpam) return $incoming_comment;
	wp_die('请勿冒充博主发表评论');
}
if(!is_user_logged_in()) add_filter( 'preprocess_comment', 'ludou_usecheck' );

// 文章目录插件
function article_index($content) {
	$matches = array();
	$ul_li = '';

	$r = "/<h3>([^<]+)<\/h3>/im";

	if(preg_match_all($r, $content, $matches)) {
		foreach($matches[1] as $num => $title) {
			$content = str_replace($matches[0][$num], '<h3 id="title-'.$num.'">'.$title.'</h3>', $content);
			$ul_li .= '<li><a href="#title-'.$num.'" title="'.$title.'">'.$title."</a></li>\n";
		}

		$content = "\n<div id=\"article-index\">
				<strong>目录</strong>
				<ul id=\"index-ul\">\n" . $ul_li . "</ul>
			</div>\n" . $content;
	}

	return $content;
}
add_filter( "the_content", "article_index" );

// 显示主题参数页面
if(is_admin()) require_once ('theme-options.php');
add_theme_support('automatic-feed-links');

/* Mini Pagenavi v1.0 by Willin Kan. */
if ( !function_exists('pagenavi') ) {
	function pagenavi( $p = 5 ) { // 取当前页前后各 2 页
		if ( is_singular() ) return; // 文章与插页不用
		global $wp_query, $paged;
		$max_page = $wp_query->max_num_pages;
		if ( $max_page == 1 ) return; // 只有一页不用
		if ( empty( $paged ) ) $paged = 1;
		if ( $paged > 1 ) p_link( $paged - 1, '上一页', '« 上一页' );/* 如果当前页大于1就显示上一页链接 */
		if ( $paged > $p + 1 ) p_link( 1, '最前页' );
		if ( $paged > $p + 2 ) echo '... ';
		for( $i = $paged - $p; $i <= $paged + $p; $i++ ) { // 中间页
			if ( $i > 0 && $i <= $max_page ) $i == $paged ? print "<span class='page-numbers current'>{$i}</span> " : p_link( $i );
		}
		if ( $paged < $max_page - $p - 1 ) echo '... ';
		if ( $paged < $max_page - $p ) p_link( $max_page, '最后页' );
		if ( $paged < $max_page ) p_link( $paged + 1,'下一页', '下一页 »' );/* 如果当前页不是最后一页显示下一页链接 */
	}
	function p_link( $i, $title = '', $linktype = '' ) {
		if ( $title == '' ) $title = "第 {$i} 页";
		if ( $linktype == '' ) { $linktext = $i; } else { $linktext = $linktype; }
		echo "<a class='page-numbers' href='", esc_html( get_pagenum_link( $i ) ), "' title='{$title}'>{$linktext}</a> ";
	}
}

// 去掉后台升级提示 Willin Kan
if ( !current_user_can('administrator') ) {
	add_filter('pre_option_update_core',    create_function('$a', "return null;"));
	add_filter('pre_option_update_plugins', create_function('$a', "return null;"));
	add_filter('pre_option_update_themes',  create_function('$a', "return null;"));

	add_filter('pre_transient_update_core',    create_function('$a', "return null;"));
	add_filter('pre_transient_update_plugins', create_function('$a', "return null;"));
	add_filter('pre_transient_update_themes',  create_function('$a', "return null;"));

	add_filter('pre_site_transient_update_core',    create_function('$a', "return null;"));
	add_filter('pre_site_transient_update_plugins', create_function('$a', "return null;"));
	add_filter('pre_site_transient_update_themes',  create_function('$a', "return null;"));

	remove_action('init', 'wp_version_check');
	remove_action('admin_init', '_maybe_update_plugins');
	remove_action('admin_init', '_maybe_update_themes');

	remove_action('admin_init', '_maybe_update_core');
	remove_action('admin_init', '_maybe_update_plugins');
	remove_action('admin_init', '_maybe_update_themes');
}

// 后台权限控制 - 投稿者和订阅者
if ( current_user_can('contributor') || current_user_can('subscriber') ) {
	add_filter('show_admin_bar','__return_false');	//隐藏管理工具栏
}

// 后台权限控制 - 作者和投稿者
if ( current_user_can('author') || current_user_can('contributor') ) {
	function custom_user() {
		remove_meta_box('postcustom','post','normal');	//移除 撰写 自定义栏目
		remove_meta_box('trackbacksdiv','post','normal');	//移除 撰写 trackback
		remove_meta_box('commentsdiv','post','normal');	//移除 撰写 评论详情
		remove_meta_box('commentstatusdiv','post','normal');	//移除 撰写 评论状态
		remove_meta_box('slugdiv','post','normal');	//移除 撰写 别名
		//remove_meta_box('fastinsert','post','normal');	//移除 撰写 FastInsert插件
		//remove_meta_box('postimagediv','post','normal');	//移除 撰写 特色图像
		remove_meta_box('categorydiv','post','normal');	//移除 撰写 分类目录
		remove_meta_box('postexcerpt','post','normal');	//移除 撰写 摘要

		//slt_removeMenu( 'edit-comments.php' );	//移除 评论
	}
	add_action('admin_init','custom_user');

	function custom_post_columns($defaults) {
		unset($defaults['comments']);	//移除 所有文章 评论列
		unset($defaults['categories']);	//移除 所有文章 分类列
		unset($defaults['author']);	//移除 所有文章 作者列
		return $defaults;
	}
	add_filter('manage_posts_columns', 'custom_post_columns');

	// 隐藏 所有文章 里的元素
	function hide_posts() {
		if ( (preg_match ('#wp-admin/edit.php(.*)$#',$_SERVER['REQUEST_URI']))) {
		?>
		<style type="text/css">.subsubsub, .alignleft, .inline-edit-col-center {display:none !important;}</style>
		<?php
		}
	}
	add_action('admin_head','hide_posts');
}

/* 后台权限控制 - 编辑
if ( current_user_can('editor') ) {
	//隐藏发布按钮
	add_action( 'admin_print_footer_scripts', 'remove_publish_button' );
	function remove_publish_button(){
		?>
		<script>jQuery(document).ready(function($){$('#publish').remove();});</script>
		<?php
	}
}
*/
/* 后台权限控制 - 投稿者
if ( current_user_can('contributor') ) {
	// 隐藏HTML编辑器下标签栏
	function hide_quicktags() {
		if ( (preg_match ('#wp-admin/post-new.php(.*)$#',$_SERVER['REQUEST_URI'])) || (preg_match ('#wp-admin/post.php(.*)$#',$_SERVER['REQUEST_URI']))	) {
		?>
		<style type="text/css">#ed_toolbar {display:none !important;}</style>
		<?php
		}
	}
	add_action('admin_head','hide_quicktags');

	// 隐藏可视化编辑器
	function custom_contributor() {
		add_filter('user_can_richedit', create_function('', 'return false;') ,50);
	}
	add_action('admin_init','custom_contributor'); 
	

	// 隐藏 控制板 概况
	function remove_right_now_widgets() {
		global $wp_meta_boxes;
		unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
	}
	add_action('wp_dashboard_setup', 'remove_right_now_widgets' );
}
*/

// 移除后台主菜单	http://sltaylor.co.uk/blog/customize-wordpress-admin-menus/
function slt_removeMenu( $item ) {
	global $menu;
	foreach ( $menu as $menuKey => $menuItem ) {
		if ( $menuItem[2] == $item ) {
			unset( $menu[$menuKey] );
			break;
		}
	}
}

// 文字截断
if ( ! function_exists( 'txtjee_substr' ) ) :
function txtjee_substr($string, $length){
	if(strlen_utf8($string) > $length) {
		return mb_substr($string, 0, $length, 'utf-8') . '…';
	} else { 
		return $string;
	}
}
endif;

// 随机函数
function randoms($stat, $end, $num) {
	$t = range($stat, $end);
	shuffle($t);
	return array_slice($t, -$num);
}

/* 表情 */
function custom_smilies_src($src, $img){
	return get_bloginfo("template_directory").'/smilies/'.$img;
}
add_filter('smilies_src', 'custom_smilies_src', 10, 2);
if ( !isset( $wpsmiliestrans ) ) {
	$wpsmiliestrans = array(
		  '/书呆子' => 'icon_see.gif',
		  '/惊叹号' => 'icon_exclamation.gif',
		  '/有木有' => 'icon_youmuyou.gif',
		  '/伤不起' => 'icon_sbq.gif',
		  '/抛媚眼' => 'icon_biggrin.gif',
		  '/流口水' => 'icon_idea.gif',
		  '/防流感' => 'icon_av.gif',
		  '/不给力' => 'icon_bgl.gif',
			'/给力' => 'icon_gl.gif',
			'/惊恐' => 'icon_shock.gif',
			'/惊讶' => 'icon_surprised.gif',
			'/大笑' => 'icon_lol.gif',
			'/愤怒' => 'icon_mad.gif',
			'/悲伤' => 'icon_sad.gif',
			'/抠鼻' => 'icon_kbz.gif',
			'/害羞' => 'icon_neutral.gif',
			'/谄笑' => 'icon_twisted.gif',
			'/不要' => 'icon_nonono.gif',
			'/阴险' => 'icon_evil.gif',
			'/可爱' => 'icon_redface.gif',
			'/烧香' => 'icon_shaoxiang.gif',
			'/尴尬' => 'icon_gg.gif',
			'/闭嘴' => 'icon_exclaim.gif',
			'/呕吐' => 'icon_vomit.gif',
			'/调皮' => 'icon_wink.gif',
			'/生病' => 'icon_sick.gif',
			  '/吃' => 'icon_mrgreen.gif',
			  '/困' => 'icon_sleepy.gif',
			  '/囧' => 'icon_arrow.gif',
			  '/笑' => 'icon_smile.gif',
			  '/酷' => 'icon_cool.gif',
			  '/色' => 'icon_razz.gif',
			  '/萌' => 'icon_rolleyes.gif',
			  '/哭' => 'icon_cry.gif',
			  '/舔' => 'icon_eek.gif',
			  '/晕' => 'icon_confused.gif',
			  '/汗' => 'icon_question.gif',
		':mrgreen:' => 'icon_mrgreen.gif',
		':twisted:' => 'icon_twisted.gif',
		   ':oops:' => 'icon_redface.gif',
		   ':evil:' => 'icon_evil.gif',
		   ':roll:' => 'icon_rolleyes.gif',
			':cry:' => 'icon_cry.gif',
			':lol:' => 'icon_lol.gif',
			  '8-)' => 'icon_cool.gif',
			  ':-(' => 'icon_sad.gif',
			  ':-?' => 'icon_confused.gif',
			  ':-)' => 'icon_smile.gif',
			  ':-o' => 'icon_surprised.gif',
			  ':-P' => 'icon_razz.gif',
			  ':-|' => 'icon_neutral.gif',
			  ';-)' => 'icon_wink.gif',
			  ':-x' => 'icon_mad.gif',
			  '8-O' => 'icon_surprised.gif',
			  ':-D' => 'icon_biggrin.gif',
			   '8)' => 'icon_cool.gif',
			   '8O' => 'icon_surprised.gif',
			   ':(' => 'icon_sad.gif',
			   ':)' => 'icon_smile.gif',
			   ':?' => 'icon_confused.gif',
			   ':D' => 'icon_biggrin.gif',
			   ':P' => 'icon_razz.gif',
			   ':o' => 'icon_surprised.gif',
			   ':x' => 'icon_mad.gif',
			   ':|' => 'icon_neutral.gif',
			   ';)' => 'icon_wink.gif',
	);
}

/* comment_mail_notify v1.0 by willin kan. (有勾選欄, 由訪客決定) */
function comment_mail_notify($comment_id) {
	$admin_notify = '1'; // admin 要不要收回覆通知 ( '1'=要 ; '0'=不要 )
	$admin_email = get_bloginfo ('admin_email'); // $admin_email 可改為你指定的 e-mail.
	$comment = get_comment($comment_id);
	$comment_author_email = trim($comment->comment_author_email);
	$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
	global $wpdb;
	if ($wpdb->query("Describe {$wpdb->comments} comment_mail_notify") == '')
		$wpdb->query("ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;");
	if (($comment_author_email != $admin_email && isset($_POST['comment_mail_notify'])) || ($comment_author_email == $admin_email && $admin_notify == '1'))
		$wpdb->query("UPDATE {$wpdb->comments} SET comment_mail_notify='1' WHERE comment_ID='$comment_id'");
	$notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : '0';
	$spam_confirmed = $comment->comment_approved;
	if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
		$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 发出点, no-reply 可改为别的 e-mail.
		$to = trim(get_comment($parent_id)->comment_author_email);
		$subject = '[' . get_option('blogname') . '] 您的评论有人回复啦！';
		$message = '
		<div style="color:#100;background-color:#f9f9f9;border:1px solid #EEE;padding:0 10px;">
			<p><strong>' . trim(get_comment($parent_id)->comment_author) . '</strong>，您好！</p>
			<p>有人回复了您在《<strong>' . get_the_title($comment->comment_post_ID) . '</strong>》的评论：</p>
		</div>
		<div><style type="text/css">.one a{display:none}.one strong{color:#4489c2}</style>
			<div class="one" style="color:#100;background-color:#f9f9f9;border:1px solid #EEE;margin:1.5em 0 1em;overflow:auto;padding:10px">
			<strong>您</strong> 的评论原文是：' . trim(get_comment($parent_id)->comment_content) . '</div>
			<div class="one" style="color:#100;background-color:#f9f9f9;border:1px solid #EEE;margin:1em 0 1.5em 30px;overflow:auto;padding:10px">
			<strong>' . trim($comment->comment_author) . '</strong> 给您的回复：' . trim($comment->comment_content) . '</div>
			<p>您可以点击 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回复完整内容</a></p>
			<p><strong>温馨提示：本邮件由系统自动发送，请勿直接回复！</strong></p>
			<p>欢迎 <a href="' . get_bloginfo('rss2_url') .'" target="_blank">订阅</a> 本站，欢迎再次光临 <a href="' . get_option('home') . '" target="_blank">' . get_option('blogname') . '</a> ！</p> 
			<p>&copy; <strong><a href="' . get_option('home') . '" target="_blank">' . get_option('blogname') . '</a></strong></p> 
		</div>';
		$from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
		$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
		wp_mail( $to, $subject, $message, $headers );
		//echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
	}
}
add_action('comment_post', 'comment_mail_notify');

/* 自動加勾選欄 */
function add_checkbox() {
	echo '<p class="comment-form-field comment_mail"><input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" /><label for="comment_mail_notify">有人回复时邮件通知我</label></p>';
}
add_action('comment_form', 'add_checkbox');
// -- END ----------------------------------------

//评论作者链接新窗口打开
function my_get_comment_author_link() { //by zwwooooo
	$url    = get_comment_author_url( $comment_ID );
	$author = get_comment_author( $comment_ID );
	if ( empty( $url ) || 'http://' == $url )
		return $author;
	else
		return "<a target='_blank' href='$url' rel='external nofollow' class='url'>$author</a>";
}
add_filter('get_comment_author_link', 'my_get_comment_author_link');

// 评论回复
if ( ! function_exists( 'txtjee_comment' ) ) :
function txtjee_comment( $comment, $args, $depth ) {
	$GLOBALS['comment'] = $comment;
	switch ( $comment->comment_type ) :
		case '' :
?>
	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
		<div id="comment-<?php comment_ID(); ?>">
			<div class="comment-info"><?php printf( __( '<cite class="fn">%s</cite>'), get_comment_author_link() );?> <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>" class="comment-meta"><?php printf( __( '%1$s at %2$s' ), get_comment_date(), get_comment_time() ) ?></a> 说:<?php edit_comment_link( __( 'Edit' ), ' ' );?></div>
			<?php if ( $comment->comment_approved == '0' ) : ?>
			<em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></em>
			<?php endif; ?>
			<div class="comment-body"><?php comment_text(); ?></div>
			<div class="reply"><?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?></div>
		</div>
	<?php
			break;
		case 'pingback'  :
		case 'trackback' :
	?>
	<li class="post pingback">
		<p><?php _e( 'Pingback:' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( 'Edit' ), ' ' ); ?></p>
	<?php
			break;
	endswitch;
}
endif;

?>